import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2022/1/17
 * Time: 9:51
 * Description: 二叉树的实现
 */
class TreeNode {
    public char val;
    public TreeNode left;//左孩子的引用
    public TreeNode right;//右孩子的引用
    public TreeNode(char val) {
        this.val = val;
    }
}
public class BinaryTree {
    //public BTNode root;//二叉树的根节点

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        //E.right = H;
        return A;
    }

    // 前序遍历
    void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //shift+f6->回车才会生效

    public List<Character> preorderTraversal(TreeNode root) {
        List<Character> retlist = new ArrayList<>();
        if(root == null) {
            return retlist;
        }
        retlist.add(root.val);
        List<Character> leftTree = preorderTraversal(root.left);
        retlist.addAll(leftTree);

        List<Character> rightTree =preorderTraversal(root.right);
        retlist.addAll(rightTree);

        return retlist;
    }

    // 中序遍历
    void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }

    /*public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> retlist = new ArrayList<>();
        if(root == null) {
            return retlist;
        }
        List<Integer> leftTree = inorderTraversal(root.left);
        retlist.addAll(leftTree);

        retlist.add(root.val);

        List<Integer> rightTree =inorderTraversal(root.right);
        retlist.addAll(rightTree);

        return retlist;
    }*/

    // 后序遍历
    void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }

    /*public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> retlist = new ArrayList<>();
        if(root == null) {
            return retlist;
        }
        List<Integer> leftTree = postorderTraversal(root.left);
        retlist.addAll(leftTree);
        List<Integer> rightTree =postorderTraversal(root.right);
        retlist.addAll(rightTree);
        retlist.add(root.val);
        return retlist;
    }*/

    /**
     *获取树中节点的个数
     */
    int count = 0;
    int size1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        count++;
        size1(root.left);
        size1(root.right);
        return count;
    }

    /**
     * 子问题思路
     * @param root
     * @return
     */
    int size(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }


    /**
     * 获取叶子节点的个数
     * 遍历思路：
     */
    static int leafCount = 0;
    void getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafCount ++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    /**
     * 获取叶子节点的个数
     * 子问题思路
     * @param root
     * @return
     */
    int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            //当前的root是叶子节点
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    /**
     * 获取第K层节点的个数
     * 子问题思路：
     *
     */
    int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null || k <= 0) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }


    /**
     * 获取二叉树的高度
     * 时间复杂度：O(n)
     * 空间复杂度：O()
     */
    int getHeight(TreeNode root) {
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight+1 : rightHeight+1;
    }

    int getHeight2(TreeNode root) {
        if(root == null) return 0;
        return getHeight2(root.left) > getHeight2(root.right) ?
                getHeight2(root.left)+1 : getHeight2(root.right)+1;
    }


        /**
         * 检测值为value的元素是否存在
         */
        TreeNode find(TreeNode root, char val) {
        if(root == null) return null;
        if(root.val == val) return root;


        TreeNode ret = find(root.left,val);
        if(ret != null) {
        return ret;
        }
        ret = find(root.right,val);
        if(ret != null) {
        return ret;
        }
        return null;
        }

        /**
         * 是不是完全二叉树
         * @param root
         * @return
         */
        boolean isCompleteTree(TreeNode root) {
        if(root == null) return true;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
        TreeNode cur = queue.poll();
        if(cur != null) {
        queue.offer(cur.left);
        queue.offer(cur.right);
        }else {
        break;
        }
        }

        while (!queue.isEmpty()) {
        TreeNode top = queue.peek();
        if(top != null) {
        return false;
        }
        queue.poll();
        }
        return true;
        }

        }